package com.expressage.controller;

import com.alibaba.fastjson.JSONArray;
import com.expressage.po.AreaAddress;
import com.expressage.po.CityAddress;
import com.expressage.service.AreaAddressService;
import com.expressage.service.CityAddressService;
import com.expressage.service.StreetAddressService;
import com.expressage.vo.AdderVO;
import com.expressage.vo.AreaAddressVO;
import com.expressage.vo.StreetAddressVO;
import com.mysql.cj.xdevapi.JsonArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 区控制器
 */
@Controller
@RequestMapping("/jspafter")
public class AreaAddressController {

    Logger logger=Logger.getLogger(AreaAddressController.class);
    @Autowired
    AreaAddressService areaAddressService;
    @Autowired
    CityAddressService cityAddressService;
    @Autowired
    StreetAddressService streetAddressService;
    @RequestMapping("/findallarea")
    public String findAllArea(Model model){
        List<AreaAddressVO> list=new ArrayList<>();
        list=areaAddressService.findAll();
        model.addAttribute("list",list);
        return "jspafter/arealist";
    }

    @RequestMapping("/findbyareaid")
    public String findByAreaid(int id,Model model){
        AreaAddressVO areaAddress=areaAddressService.findByAreaid(id);
        model.addAttribute("areaAddress",areaAddress);
        return "";
    }
    @RequestMapping(value="/jfindbycityid",produces={"text/html;charset=UTF-8;","application/json;"})
    public @ResponseBody String JfindByCityid( int id){

        List<AreaAddress> list=new ArrayList<>();
        list=areaAddressService.findByCityid(id);
        return JSONArray.toJSONString(list);
    }


    @RequestMapping("/deletearea")
    public String deletearea(int[] id){

        int id1=id[0];

        AreaAddressVO areaAddress= areaAddressService.findByAreaid(id1);

        areaAddress.setAreaname("删除状态");

        boolean flag=areaAddressService.update(areaAddress);
        if(flag){
            return  "redirect:findallarea.action";
        }else {
            return "";
        }


    }
    @RequestMapping("/toupdatearea")
    public String toupdateArea(int id,Model model){
        AreaAddressVO areaAddress=areaAddressService.findByAreaid(id);
        model.addAttribute("areaAddress",areaAddress);
        return "jspafter/updatearea";
    }
    @RequestMapping("/updatearea")
    public String updateArea(AreaAddressVO areaAddress){
        String  cityName= areaAddress.getCityname();
        CityAddress cityAddress = cityAddressService.findByCityName(cityName);
        areaAddress.setCityid(cityAddress.getCityid());
        boolean flag=areaAddressService.update(areaAddress);
        if(flag){
            return  "redirect:findallarea.action";
        }else {
            return "";
        }
    }


    @RequestMapping("/toinsertarea")
    public String toAddarea(Model model){

        /*List<AreaAddressVO> list1=new ArrayList<>();
        list1=areaAddressService.findAll();
        model.addAttribute("list1",list1);*/
        List<CityAddress> list2=new ArrayList<>();
        list2=cityAddressService.findAll();
        model.addAttribute("list2",list2);

        return "jspafter/addarea" ;
    }
    @RequestMapping("/insertarea")
    public String addStreet(AreaAddressVO  areaAddress){
       boolean flag= areaAddressService.insert(areaAddress);

        if(flag){
            return  "redirect:findallarea.action";
        }else {
            return "";
        }
    }
    @RequestMapping("/tojson")
    public String toJson(Model model){
        List<CityAddress> list2=new ArrayList<>();
        list2=cityAddressService.findAll();
        model.addAttribute("list2",list2);
        return "jspafter/sanjicha";
    }

}
